from fastapi import APIRouter, Depends, Body
from router.route_log import LogRoute
from model.Role import RoleModel
from model.User import UserModel
from common.db_init import get_db
from common.response import ApiResponse, CustomApiResponse, response_code, responseCode
from sqlalchemy.orm import Session
from utils.tool import Page
from paramsModel.role import RoleParams
from typing import List

roleController = APIRouter(prefix='/role',route_class=LogRoute)


@roleController.get('/list', response_model=CustomApiResponse)
async def get_list(page: int = 1, pagesize: int = 10, db: Session = Depends(get_db)):
    pageModel = Page(page=page, pagesize=pagesize)
    rolelist = RoleModel.get_list(db=db, page={'start': pageModel.skip, 'pagesize': pagesize})
    return CustomApiResponse(data=rolelist)


@roleController.get('/all', response_model=CustomApiResponse)
async def get_list(db: Session = Depends(get_db)):
    rolelist = RoleModel.get_all(db=db)
    return CustomApiResponse(data=rolelist)


@roleController.post('/add', response_model=CustomApiResponse)
async def addItem(roleinfo: RoleParams, db: Session = Depends(get_db), currentUser=Depends(UserModel.get_current_user)):
    RoleModel.add(db=db, addinfo=roleinfo, currentUser=currentUser)
    return CustomApiResponse()


@roleController.post('/dels', response_model=CustomApiResponse)
async def delItems(params=Body(...), db: Session = Depends(get_db), currentUser=Depends(UserModel.get_current_user)):
    RoleModel.dels(db=db, uids=params)
    return CustomApiResponse()


@roleController.post('/edit',response_model=CustomApiResponse)
async def edit(params=Body(default={}),db: Session = Depends(get_db)):
    RoleModel.editByItem(db=db,item=params)
    return CustomApiResponse()